using System;
using System.Collections.Generic;
using System.Text;
using PlugNT.Cms.Custom;

namespace PlugNT.Cms.Page
{



    public class AutoUpdater
    {

        private static string datespan = "";




        public static void Start()
        {

            if (WebConfig.IsAutoUpdate)
            {
                Exec();
            }
            else
            {
                Abort();
            }
        }




        public static void Abort()
        {
            if (timerClock != null)
            {
                timerClock.Enabled = false;
            }
        }




        private static void Dispose()
        {
            if (timerClock != null)
            {
                timerClock.Enabled = false;
                timerClock.Dispose();
                timerClock = null;
            }
        }






        private static System.DayOfWeek GetWeekByIntString(string intString)
        {
            System.DayOfWeek week = System.DayOfWeek.Monday;
            switch (intString)
            {
                case "1":
                    week = System.DayOfWeek.Monday;
                    break;
                case "2":
                    week = System.DayOfWeek.Tuesday;
                    break;
                case "3":
                    week = System.DayOfWeek.Wednesday;
                    break;
                case "4":
                    week = System.DayOfWeek.Thursday;
                    break;
                case "5":
                    week = System.DayOfWeek.Friday;
                    break;
                case "6":
                    week = System.DayOfWeek.Saturday;
                    break;
                case "7":
                    week = System.DayOfWeek.Sunday;
                    break;
                default:
                    week = System.DayOfWeek.Monday;
                    break;
            }
            return week;
        }




        private static System.Timers.Timer timerClock = null;
        private static void OnTimer(Object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                DateTime dt = DateTime.Now;
                if (dayFlag == "0" || autoWeek == dt.DayOfWeek)
                {
                    if (autoTimeString == dt.ToString("H-m-s"))
                    {
                        PageBuilder.UpdatePages();
                    }
                }
            }
            catch
            {
                timerClock.Enabled = false;
            }
        }

        private static System.DayOfWeek autoWeek;
        private static string dayFlag = "";
        private static string autoTimeString = "";



        private static void Exec()
        {
            datespan = WebConfig.AutoDateSpan;
            dayFlag = datespan.Substring(0, 1);
            autoWeek = GetWeekByIntString(dayFlag);
            autoTimeString = datespan.Substring(2);

            if (timerClock == null)
            {
                timerClock = new System.Timers.Timer();
                timerClock.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
            }
            timerClock.Interval = 1000;
            timerClock.Enabled = true;

        }




    }
}

